<?php
class AdminAction extends CommonAction
{
	public function validate() {
		
	}
	
	public function index() {
		$Model = D('Admin');
		$map['type'] = array('neq', 0); 
		$this -> _list( $Model, $map, true );
	
		$this -> display();
	}
	
	public function add() {
		$types = AdminModel::$types;
		$this -> assign('types', $types);
		$this -> display();
	}
	
	/**
	 * 字段验证
	 * @see CommonAction::insertCheck()
	 */
	protected function insertCheck($Model) {
		$account = trim( $this -> _param('account') );
		$ret = $Model -> where('account="'.$account.'"') -> find();
		if ($ret)
			$this -> error('已存在相同的用户名！');
		
		$pwd = $this -> _param('pwd');
		$repwd = $this -> _param('repwd');
		if ($pwd != $repwd)
			$this -> error('两次密码不一致！');
	}
	
	/**
	 * 输入扩展字段
	 * @see CommonAction::insertExt()
	 */
	protected function insertExt($Model) {
		$Model -> create_by = $this -> getLoginId();
		$Model -> create_time = time();
		$Model -> pwd = md5( trim( $Model -> account ) . C('PWD_LINK') . trim( $Model -> pwd ) );
	}
	
	/**
	 * 输出扩展字段
	 * @see CommonAction::outputExt()
	 */
	protected function outputExt($list) {
		if ($list) {
			$admins = D('Admin') -> getAdmins('', 'id,account');
			foreach($list as $key => $val) {
				if (isset($val['create_time']))
					$val['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
				if (isset($val['last_login_time']))
					$val['last_login_time'] = date('Y-m-d H:i:s', $val['last_login_time']);
				if (isset($val['create_by']))
					$val['create_by'] = $admins[$val['create_by']]['account'];
				
				$list[$key] = $val;
			}
		}
		
		return $list;
	}
	
}